package com.twg.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class utilJDBC {
		//ȫ�ֱ���
		private static String url="jdbc:mysql://localhost:3306/tong";
		private static String user="root";
		private static String pwd="roor";
		private static Connection con=null;
		private static PreparedStatement ps=null;
		private static ResultSet rs=null;
		//�Ż���
	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
		//�Ż����������븳ֵ
	public static void getcon(String sql,Object[] obj){
		try {
			con=DriverManager.getConnection(url, user, pwd);
			ps=con.prepareStatement(sql);
			for(int i=0;i<obj.length;i++){
				ps.setObject(i+1, obj[i]);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
		//��ɾ��
	public static int update(String sql,Object[] obj){
		int i=0;
		getcon(sql,obj);
		try {
			i=ps.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			close();
		}
		return i;
	}
		//��ѯ
	public static ResultSet query(String sql,Object[] obj){
		getcon(sql, obj);
		try {
			rs=ps.executeQuery();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	//�ر���Դ
	public static void close(){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(ps!=null){
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(con!=null){
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
}
